package cn.io1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test01 {
    /**
     * 练习1：拷贝文件夹
     * @param args
     */
    public static void main(String[] args) {
        copyAll(new File("move/src"),new File("move/dest"));
    }

    private static void copyAll(File src,File dest){
        dest.mkdirs();
        File[] files = src.listFiles();
        if (files != null){
            for (File file : files) {
                if (file.isFile()){
                    try (
                        FileInputStream fis = new FileInputStream(file);
                        FileOutputStream fos = new FileOutputStream(new File(dest,file.getName()))
                    ){
                        byte[] bytes = new byte[1024];
                        int len;
                        while ((len = fis.read(bytes))!=-1){
                            fos.write(bytes,0,len);
                        }
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }else{
                    copyAll(file,new File(dest,file.getName()));
                }
            }
        }
    }
}
